/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tutor;

import common.Constants;
import ejb.GestioneFileLocal;
import ejb.GestoreTutorLocal;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jpa.ApprovazioneDocumento;
import jpa.Documento;
import jpa.Tutor;
import jpa.Upload;
import org.apache.commons.lang3.StringUtils;
import studente.util.MultipartRequest;

/**
 *
 * @author Administrator
 */
public class UploadFileTutor extends HttpServlet {

    @EJB
    GestioneFileLocal gfl;
    @EJB
    GestoreTutorLocal gtl;
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        MultipartRequest multi = new MultipartRequest(request, ".");
        File f = multi.getFile("file"); // max 10 Mega
        String fileName = multi.getFilesystemName("file");
        String titolo = multi.getParameter("titolo").toString();
        String descrizione = multi.getParameter("decrizione").toString();
        String id = request.getSession(false).getAttribute("id").toString();
        Date data = new Date();
        if (StringUtils.isBlank(titolo) || StringUtils.isBlank(descrizione) || StringUtils.isBlank(fileName)) {
            response.sendRedirect("GesFileTutor_2_errore.jsp");
        }

        if (f != null) {
            String link = Constants.FILE_DIR + fileName;
            File fOUT = new File(link);
            FileOutputStream fOS;
            try {
                FileInputStream fIS = new FileInputStream(f);
                fOS = new FileOutputStream(fOUT);
                while (fIS.available() > 0) {
                    fOS.write(fIS.read());
                }
                fOS.close();

                Integer idTutor = new Integer(id);
                Tutor tutor = gtl.getTutor(idTutor);
                Upload up = new Upload();
                up.setTutorid(idTutor);
                up.setData(data);
                Documento doc = new Documento();
                doc.setTitolo(titolo);
                doc.setLink(link);
                doc.setDescr(descrizione);
                doc.setCorsiid(tutor.getCorsiid());
                ApprovazioneDocumento ad = new ApprovazioneDocumento();
                ad.setData(data);
                ad.setTutorid(new Integer(id));
                boolean ris = gfl.uploadTutor(up, ad, doc);
                if (ris) {
                    response.sendRedirect("GesFileTutor_2_ok.jsp");
                } else {
                    response.sendRedirect("GesFileTutor_2_errore.jsp");
                }
            } catch (Exception e) {
                response.sendRedirect("GesFileTutor_2_errore.jsp");
            }

        } else {
            response.sendRedirect("GesFileTutor_2_errore.jsp");
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
